package com.carcenter.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 - 消息队列的配置类
 */
@Configuration
public class RabbitConfig {


    //普通预约队列，指定死信交换机和路由键
    @Bean
    public Queue appointmentQueue() {
        return QueueBuilder.durable("appointment.queue")  //预约队列
                .deadLetterExchange("appointment.dlx.exchange")  //设置死信交换机
                .deadLetterRoutingKey("appointment.key.dlx")      //设置死信路由键
                .ttl(1 * 10* 1000)   //默认是40分钟 (开发阶段10秒)
                .maxLength(30000)   //最大长度
                .build();
    }

    //普通交换机
    @Bean
    public DirectExchange appointmentExchange() {
        return new DirectExchange("appointment.exchange");
    }

    //普通预约队列绑定普通交换机
    @Bean
    public Binding appointmentBinding(Queue appointmentQueue, DirectExchange appointmentExchange) {
        return BindingBuilder.bind(appointmentQueue).to(appointmentExchange).with("appointment.key.normal");
    }

    //死信队列
    @Bean
    public Queue appointmentDlxQueue() {
        return new Queue("appointment.dlx.queue");
    }

    //死信交换机
    @Bean
    public DirectExchange appointmentDlxExchange() {
        return new DirectExchange("appointment.dlx.exchange");
    }

    //死信队列绑定死信交换机
    @Bean
    public Binding appointmentDlxBinding(Queue appointmentDlxQueue, DirectExchange appointmentDlxExchange) {
        return BindingBuilder.bind(appointmentDlxQueue).to(appointmentDlxExchange).with("appointment.key.dlx");
    }


    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}